package org.yangspring.samples.mvc.simple;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.yangspring.samples.mvc.domain.Company;
import org.yangspring.samples.mvc.service.CompanyService;

/**
 * Created by whyang on 2017/06/19.
 */

@Controller
//@RestController
@RequestMapping("/company")
public class CompanyController {
    private static final Logger logger = LoggerFactory.getLogger(AnimalController.class);

    /*private CompanyService companyService;

    @Autowired
    @Qualifier(value = "companyService")
    public void setCompanyService(CompanyService service){
        this.companyService = service;
    }*/

    @Autowired
//    @Resource(name = "companyService")
    private CompanyService companyService;

    @RequestMapping(value = "/list")
    public String listCompany(Model model){
        model.addAttribute("company001", new Company());
        model.addAttribute("listCompany", this.companyService.listCompany());
        return "listCompany";
    }

    @RequestMapping(value = "add")
    public String addCompany(@ModelAttribute("company001") Company company){
        this.companyService.addCompany(company);
        return "addCompany";
    }

    @RequestMapping("/remove/{id}")
    public String removeCompany(@PathVariable("id") int id){

        this.companyService.removeCompany(id);
        return "redirect:/company";
    }

    @RequestMapping("/edit/{id}")
    public String editPerson(@PathVariable("id") int id, Model model){
        model.addAttribute("company", this.companyService.getCompanyById(id));
        model.addAttribute("listCompany", this.companyService.listCompany());
        return "company";
    }

}
